/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;
import Controller.SejourController;
import Model.Agenda;
import Model.Reservation;
import Model.Sejour;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import java.util.Date;
/**
 *
 * @author Hugo
 */
public class DemarrerSejour extends AbstractViewPanel {

     private SejourController controller;
    
    /**
     * Creates new form DemarrerSejour1
     */
    public DemarrerSejour(SejourController controller) {
        this.controller = controller;
        initComponents();

        this.rdbNumConf.setSelected(true);
        this.txtDateDebut.setEnabled(false);
        this.txtDateDepart.setEnabled(false);
        this.txtNomClient.setEnabled(false);
        this.txtNumTel.setEnabled(false);
        this.txtNumConf.setEnabled(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGrpClient = new javax.swing.ButtonGroup();
        jTextField1 = new javax.swing.JTextField();
        btnOk = new javax.swing.JButton();
        rdbNumConf = new javax.swing.JRadioButton();
        rdbNom = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        rdbDate = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        txtNumConf = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtNomClient = new javax.swing.JTextField();
        txtNumTel = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtDateDebut = new javax.swing.JTextField();
        txtDateDepart = new javax.swing.JTextField();

        jTextField1.setText("jTextField1");

        btnOk.setText("OK");
        btnOk.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnOkMouseClicked(evt);
            }
        });

        btnGrpClient.add(rdbNumConf);
        rdbNumConf.setText("Utiliser le numéro de confirmation");
        rdbNumConf.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rdbNumConfMouseClicked(evt);
            }
        });
        rdbNumConf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rdbNumConfActionPerformed(evt);
            }
        });

        btnGrpClient.add(rdbNom);
        rdbNom.setText("Le client à oublier son numéro");
        rdbNom.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rdbNomMouseClicked(evt);
            }
        });

        jLabel5.setText("Date Départ:");

        btnGrpClient.add(rdbDate);
        rdbDate.setText("Le client n'a pas de réservation");
        rdbDate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rdbDateMouseClicked(evt);
            }
        });

        jLabel1.setText("Numero de confirmation:");

        jLabel4.setText("Date Début:");

        jLabel3.setText("Nom du client:");

        jLabel6.setText("Numéro de téléphone:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(40, 40, 40)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtNumConf, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel6)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtNumTel, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(24, 24, 24)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(rdbNumConf)
                                .addComponent(rdbNom)
                                .addComponent(rdbDate))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtDateDebut, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                            .addComponent(txtDateDepart))))
                .addContainerGap(52, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(111, 111, 111))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(rdbNumConf)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumConf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(rdbNom)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addComponent(rdbDate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtDateDebut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDateDepart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnOk)
                .addGap(22, 22, 22))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void rdbNumConfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdbNumConfActionPerformed
            
    }//GEN-LAST:event_rdbNumConfActionPerformed

    private void rdbNumConfMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rdbNumConfMouseClicked
        if(this.rdbNumConf.isSelected()){
            this.txtDateDebut.setEnabled(false);
            this.txtDateDepart.setEnabled(false);
            this.txtNomClient.setEnabled(false);
            this.txtNumTel.setEnabled(false);
            this.txtNumConf.setEnabled(true);
        }
    }//GEN-LAST:event_rdbNumConfMouseClicked

    private void rdbNomMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rdbNomMouseClicked
        if(this.rdbNom.isSelected()){
            this.txtDateDebut.setEnabled(false);
            this.txtDateDepart.setEnabled(false);
            this.txtNomClient.setEnabled(true);
            this.txtNumTel.setEnabled(true);
            this.txtNumConf.setEnabled(false);
        }
    }//GEN-LAST:event_rdbNomMouseClicked

    private void rdbDateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rdbDateMouseClicked
        if(this.rdbDate.isSelected()){
            this.txtDateDebut.setEnabled(true);
            this.txtDateDepart.setEnabled(true);
            this.txtNomClient.setEnabled(false);
            this.txtNumTel.setEnabled(false);
            this.txtNumConf.setEnabled(false);
        }
    }//GEN-LAST:event_rdbDateMouseClicked

    private void btnOkMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnOkMouseClicked
        try{
            int num = Agenda.getInstance().getNextIdSejour();
            if(this.rdbNumConf.isSelected()){
                Reservation r = Agenda.getInstance().TrouverReservation(Integer.parseInt(this.txtNumConf.getText()));

                if (r!= null){
                    this.txtNumConf.setText("");
                    Sejour s = new Sejour(num);
                    this.controller.addSejour(s);
                    this.controller.addModel(s);
                    InfoReservation infoReservation = new InfoReservation(r, this.controller,num);
                    this.controller.addView(infoReservation);
                    JFrame displayFrame = new JFrame("Informations sur la réservation");
                    displayFrame.getContentPane().add(infoReservation, BorderLayout.CENTER);
                    displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                    displayFrame.pack();
                    displayFrame.setVisible(true);
                    this.getTopLevelAncestor().setVisible(false);
                }
                else
                    JOptionPane.showMessageDialog(this, "La réservation n'a pas été trouvée.");
            }
            if(this.rdbNom.isSelected()){
                Reservation r = Agenda.getInstance().TrouverReservation(this.txtNomClient.getText(), this.txtNumTel.getText());

                if (r!= null){
                    this.txtNomClient.setText("");
                    this.txtNumTel.setText("");
                    Sejour s = new Sejour(num);
                    this.controller.addSejour(s);
                    this.controller.addModel(s);
                    InfoReservation infoReservation = new InfoReservation(r,this.controller,num);
                    this.controller.addView(infoReservation);
                    JFrame displayFrame = new JFrame("Informations sur la réservation");
                    displayFrame.getContentPane().add(infoReservation, BorderLayout.CENTER);
                    displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                    displayFrame.pack();
                    displayFrame.setVisible(true);
                    this.getTopLevelAncestor().setVisible(false);
                }
                else
                    JOptionPane.showMessageDialog(this, "La réservation n'a pas été trouvée.");
            }
            if(this.rdbDate.isSelected()){
                this.txtNumTel.setText("");
                this.txtNomClient.setText("");
                Sejour s = new Sejour(num);
                this.controller.addSejour(s);
                this.controller.addModel(s);
                String dteDebut = this.txtDateDebut.getText();
                String dteFin = this.txtDateDepart.getText();
                FournirChambre fournirChambreVue = new FournirChambre(controller, getDate(dteDebut),getDate(dteFin),num);
                this.controller.addView(fournirChambreVue);
                JFrame displayFrame = new JFrame("Fournir les chambres au clients");
                displayFrame.getContentPane().add(fournirChambreVue, BorderLayout.CENTER);
                displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                displayFrame.pack();
                displayFrame.setVisible(true);
                this.getTopLevelAncestor().setVisible(false);
            }
        }catch( NullPointerException e){
            //JOptionPane.showMessageDialog(this, "Vous ne pouver partir un séjour avant la date d'aujourd'hui.");
        }
        catch (Exception e){
            JOptionPane.showMessageDialog(this, "La réservation n'a pas été trouvée.");
        }
    }//GEN-LAST:event_btnOkMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGrpClient;
    private javax.swing.JButton btnOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JRadioButton rdbDate;
    private javax.swing.JRadioButton rdbNom;
    private javax.swing.JRadioButton rdbNumConf;
    private javax.swing.JTextField txtDateDebut;
    private javax.swing.JTextField txtDateDepart;
    private javax.swing.JTextField txtNomClient;
    private javax.swing.JTextField txtNumConf;
    private javax.swing.JTextField txtNumTel;
    // End of variables declaration//GEN-END:variables

private Date getDate(String date){
        Date now = Calendar.getInstance().getTime();
        Date dteFormatte = new Date();
        boolean isRealDate = true;
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
        dateFormat.setLenient(false);
        
        try{
            dteFormatte = dateFormat.parse(date);
                if(isRealDate){
                    if(dteFormatte.before(now)){
                        isRealDate = false;
                        JOptionPane.showMessageDialog(this, "Veuillez entré une date après aujourd'hui", "La date entré n'est pas bonne", 0);
                    }
                }
        }catch (ParseException e){
            JOptionPane.showMessageDialog(this, "Veuillez entré une date du format dd/MM/yyyy.", "La date entré n'est pas bonne", 0);
            isRealDate = false;
        }
       if(isRealDate)
            return dteFormatte;
       else
            return null;
    }


    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
         
    }
}
